/**
 * @author hequan
 */
$(document).ready(function(){
	$(window).on('load',function(){
		imgLocation();
		var dataImg={'data':[{'src':'11.jpg'},{'src':'12.jpg'},{'src':'13.jpg'},{'src':'12.jpg'},{'src':'11.jpg'}]};
		window.onscroll=function(){
			if(scrollSide()){
				$.each(dataImg.data,function(index,value){
					var box=$("<div>").addClass('box').appendTo($('#container'));
					var content=$("<div>").addClass('content').appendTo(box);
					//console.log('img/'+$(value).attr('src'));
					$('<img>').attr('src','img/'+$(value).attr('src')).appendTo(content);
					imgLocation();
				})
			}
		}
		
	})
});
function scrollSide(){
	var box=$('.box');
	var lastboxHeight=box.last().get(0).offsetTop+Math.floor(box.last().height()/2);
	//console.log(lastboxHeight);
	var documentHeight=$(document).height();
	var scrollHeight=$(window).scrollTop();
	console.log(scrollHeight);
	return(lastboxHeight<documentHeight)?true:false;
}
function imgLocation(){
	var box=$('.box');
	var boxWidth=box.eq(0).width();
	var num=Math.floor($(window).width()/boxWidth);
	var boxArr=[];
	
	
	box.each(function(index,value){
		var boxHeight=box.eq(index).height();
		if(index<num){
			boxArr[index]=boxHeight;
			//console.log(boxHeight);
		}else{
			var minboxHeight=Math.min.apply(null,boxArr);
			//console.log(minboxHeight);
			var minboxIndex=$.inArray(minboxHeight,boxArr)
			//console.log(minboxIndex);
			//console.log(value);
				$(value).css({
				'position':'absolute',
				'top':minboxHeight,
				'left':box.eq(minboxIndex).position().left
			});
			
			boxArr[minboxIndex]+=box.eq(index).height();
			//console.log(boxArr[minboxIndex]);
		
		}
	})
	
}
